package com.wen.tian.controller;

import com.wen.tian.bean.ResultUtil;
import com.wen.tian.enums.CommonEnum;
import com.wen.tian.exception.CustomException;
import com.wen.tian.utils.ServletUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * springboot全局异常处理
 *
 * @author tww
 * ClassName: GlobalExceptionHandler.java、
 * createTime: 2020年11月04日 16:09:00
 **/
@ControllerAdvice
public class GlobalExceptionHandler {


    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = CustomException.class)
    @ResponseBody
    public Object CustomExceptionHandler(HttpServletRequest request, CustomException e) {
        logger.error("发生业务异常！原因是：{}", e.getCommonEnum().getResultMsg());
        if (ServletUtils.isAjaxRequest(request)) {
            return ResultUtil.error(e.getCommonEnum().getResultCode(), e.getCommonEnum().getResultMsg());
        }
        ModelAndView modelAndView = new ModelAndView("error/business");
        modelAndView.addObject("e", e);
        return modelAndView;
    }


    /**
     * 处理空指针的异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public Object exceptionHandler(HttpServletRequest request, NullPointerException e) {
        logger.error("发生空指针异常！原因是:", e);
        if (ServletUtils.isAjaxRequest(request)) {
            return ResultUtil.error(CommonEnum.NULL);
        }
        ModelAndView modelAndView = new ModelAndView("error/business");
        modelAndView.addObject("e", e);
        return modelAndView;
    }

    /**
     * 处理空指针的异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = AuthenticationException.class)
    @ResponseBody
    public Object exceptionHandler(HttpServletRequest request, AuthenticationException e) {
        logger.error("发生空指针异常！原因是:", e);
        if (ServletUtils.isAjaxRequest(request)) {
            return ResultUtil.error(CommonEnum.UNKNOWN_USER_OR_PASSWORD);
        }
        ModelAndView modelAndView = new ModelAndView("error/business");
        modelAndView.addObject("e", e);
        return modelAndView;
    }


    /**
     * 处理其他异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Object exceptionHandler(HttpServletRequest request, Exception e) {
        logger.error("未知异常！原因是:", e);
        if (ServletUtils.isAjaxRequest(request)) {
            return ResultUtil.error(CommonEnum.INTERNAL_SERVER_ERROR);

        }

        ModelAndView modelAndView = new ModelAndView("error/business");
        modelAndView.addObject("e", e);
        return modelAndView;
    }


}
